package com.tanhua.server.controller;

import com.tanhua.model.domain.mysql.UserInfo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author lk
 * @date 2022-03-24
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserInfoService userInfoService;

    /**
     * 保存用户信息
     *   UserInfo
     *   请求头中携带token
     */
    @PostMapping("/loginReginfo")
    public ResponseEntity loginReginfo(@RequestBody UserInfo userInfo){
        //获取id    Claims本质是map
        Long userId = UserHolder.getUserId();
        //向userinfo中设置用户id
        userInfo.setId(Long.valueOf(userId));
        //调用service
        userInfoService.save(userInfo);
        return ResponseEntity.ok(null);
    }
        /**
         * 更新用户资料
         */
        @PutMapping("/loginReginfo/head")
        public ResponseEntity updateUserInfo(@RequestBody MultipartFile headPhoto, @RequestHeader("Authorization") String token) throws IOException {
            //获取id    Claims本质是map
            Long userId = UserHolder.getUserId();
            userInfoService.updateHead(headPhoto,Long.valueOf(userId));
            return ResponseEntity.ok(null);
        }

}
